<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 用户管理
 */
class JubaoController extends BaseController
{
    /**
     * 用户列表
     * @return [type] [description]
     */
    public function index($key="")
    {
		$where = array();
        if($key == ""){
            $model = M('jubao');
              $list = M('list');
        }else{
            //$where['username'] = trim($key);
            //$model = M('jubao')->where($where);
        }


        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        //$member = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $member = $model->join('LEFT JOIN lc_list on lc_jubao.appid =  lc_list.id' )->field('lc_jubao.*,lc_list.hslogo,lc_list.status')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('lc_jubao.id DESC')->select();
     //   $counts = $model->join('LEFT JOIN lc_list on  lc_jubao.appid =  lc_list.id,' )->where($where)->count();


        $this->assign('member', $member);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 更新管理员信息
     * @param  [type] $id [管理员ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $model = M('admin')->find(I('id'));
            $this->fid = session('adminId');
            $this->assign('model',$model);
            $this->display();
        }
        if (IS_POST) {

            $_POST['tianshu'] = strtotime($_POST['tianshu']);
            $model = D("Admin");
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
                //验证密码是否为空
                $data = I();
                unset($data['password']);
                if(I('password') !== ""){
                    $data['password'] = md5(I('password'));
                } else {
                    $data['password'] = $model->where(array('id'=>I('id')))->getField('password');
                }
                //强制更改超级管理员用户类型
                if(C('SUPER_ADMIN_ID') == I('id')){
                    $data['type'] = 2;
                }

                //给会员增加天数
                if ($_POST['gts'] == 0) {
                    $_POST['create_time'] =  time();
                }

                //var_dump($data);die;
                $info = $model->data($data)->save();
                //var_dump($info);die;
                //更新
                if ($info !== false) {
                    $this->success("用户信息更新成功", U('admin/index'));
                } else {
                    $this->error("未做任何修改,用户信息更新失败");
                }
            }
        }
    }

    /**
     * 删除管理员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
    	if(C('SUPER_ADMIN_ID') == $id) $this->error("超级管理员不可禁用!");
        $model = M('admin');
        //查询status字段值
        $result = $model->find($id);
        //更新字段
        $data['id']=$id;
        if($result['status'] == 1){
        	$data['status']=0;
        }
        if($result['status'] == 0){
        	$data['status']=1;
        }
        if($model->save($data)){
            $this->success("状态更新成功", U('admin/index'));
        }else{
            $this->error("状态更新失败");
        }
    }

    /**
     * 批量删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function alldel($id) {
            if (empty($id)) {
	    $this->error("不能提交空值!");
        }
        //解析参数
        $id=$this->jiexi($id);
        $member = M("jubao");
        $result = $member->where("id in($id)")->field()->select();
        $memenum = $member->where("id in($id)")->delete($id);
        if($memenum){
            $this->success("删除成功", U('jubao/index'));
        }else{
            $this->error("删除失败", U('jubao/index'));
        }
    }
    public function forbidden($id){
        $id=$this->jiexi($id);
        $status = I("type");
        $model = M('jubao');
        $list = M('list');
      //  $model = $jubao->where("id in($id)")->field('appid')->select();
        $member = $model->join('LEFT JOIN lc_list on lc_jubao.appid =  lc_list.id' )->field('lc_list.id')->where("lc_jubao.id in($id)")->select();


        //查询status字段值
        /*
         * 判断传入的 类型是否为空字串
         * 默认:0,1:新提交；2：待处理；3：已完成;4:封禁 ; 由于是后期更改数据库字段所有不清楚那个是禁用字段所以用status字段
         * 后期在修改
        */
        if(!empty($status)){
            if($status == 0){
                $data['status']=4;
            }
            if($status == 4){
                $data['status']=0;
            }
        }else{
            $result = $model->join('LEFT JOIN lc_list on lc_jubao.appid =  lc_list.id' )->where("lc_jubao.id in($id)")->field('lc_list.id,lc_list.status')->select();
            //更新字段
            $data['status']="";
            foreach ($result as $index => $item) {
                if($item['status'] == 3){
                    $data['status']=4;
                }

            if($item['status'] == 0){
                $data['status']=4;
            }
            }
        }
        $arr = "";
        foreach ($member as $key => $val) {

                $arr .= $val['id'].',';
        }
        $a= rtrim($arr,',');


        if($list->where("id in($a)")->save($data)){
            $this->success("禁用更新成功", U('jubao/index'));
        }else{
            $this->error("禁用更新失败",U('jubao/index'));
        }
    }


    function jiexi($id){
        //解析参数
        $arr = explode(",",$id);
        //如果有多余字符去掉
        $arr = array_merge(array_diff($arr, array("undefined")));
        $arr = array_merge(array_diff($arr, array("on")));

        if(is_array($arr)){
            return  $id = implode(',',$arr);
        }else{
            return   $id = 'id='.$arr;
        }
	 }
}
